/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package planspiel;

import admintool.MatchRatingCalculator;
import admintool.imp03_data.IMP03_Candidate;
import admintool.imp03_data.IMP03_Vacancy;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import planspiel.panels.GameScreen;

/**
 *
 * @author Administrator
 */
public final class Game {

    private ArrayList<GameRoundController> gameRoundControllers = new ArrayList<GameRoundController>();
    private JPanel mainPanel = null;
    private PlanspielView planspielView = null;
    private JDialog summaryDialog = null;
    private GameRound gameRound = null;
    
    /**
     * 
     * @param mainPanel
     * @param planspielView
     */
    public Game(JPanel mainPanel, PlanspielView planspielView) {
        this.mainPanel = mainPanel;
        this.planspielView = planspielView;

        gameRound = new GameRound(GameRoundController.VACANCYSTAGE);
        gameRoundControllers.add(getInitialGameRoundController(gameRound.getRoundID()));
        
        initDisplay();
    }
    
    /**
     * 
     */
    public void finishRound(){
        int currentRoundID = getCurrentGameRoundController().getRoundID();
        IMP03_Candidate selectedCadidate = getCurrentGameRoundController().getSelectedCandidate();
        IMP03_Vacancy selectedVacancy = getCurrentGameRoundController().getSelectedVacancy();
        
        MatchRatingCalculator matchRatingCalc = new MatchRatingCalculator(selectedCadidate, selectedVacancy);
        
        int candidateCountForThisPlatform = IMP03_Candidate.getAllAvailableCandidatesForSpecificPlatform(getCurrentGameRoundController().getSelectedPlatform()).size();
        int placeOfMyCandidate = matchRatingCalc.whichPlaceHasMyCandidateInComparisionToTheRest(getCurrentGameRoundController().getSelectedPlatform());
        
        {
            summaryDialog = new JDialog();
            summaryDialog.setModal(true);
            summaryDialog.setTitle("Summary of the round");
            
            GroupLayout layout = new GroupLayout(summaryDialog.getContentPane());
            layout.setAutoCreateGaps(true);
            summaryDialog.setLayout(layout);
            
            JLabel candidateMatchingRate = new JLabel("MatchingRate : " + matchRatingCalc.getCandidateVacancyMatch() + "%. This is the " + placeOfMyCandidate + ". place of " + candidateCountForThisPlatform); 
            JLabel matchRatingDescription = new JLabel("<html><body>"
                    + "Distance [10%] = " + matchRatingCalc.getDistanceFactor() + "% - distance between the vacancy and the candidate of your choice: " + Math.round(matchRatingCalc.calculateDistance() * 100) / 100 + "km<br>"
                    + "Experience [20%] = " + matchRatingCalc.getExperienceFactor() + "%<br>"
                    + "Qualification [30%] = " + matchRatingCalc.getQualificationFactor() + "%<br>"
                    + "SkillSet [40%] = " + matchRatingCalc.getSkillFactor() + "%<br>"
                    + "</body></html>");

            JButton ok = new JButton("ok");
            ok.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    summaryDialog.dispose();
                }
            });
            
            // layout-definition
            layout.setHorizontalGroup(
                layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(candidateMatchingRate)
                    .addComponent(matchRatingDescription)
                    .addComponent(ok, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            );

            layout.setVerticalGroup(
                layout.createSequentialGroup()
                    .addComponent(candidateMatchingRate)
                    .addComponent(matchRatingDescription)
                    .addComponent(ok)
            );

            summaryDialog.pack();
            summaryDialog.setLocationRelativeTo(mainPanel);
            summaryDialog.setVisible(true);
        }
        
        gameRoundControllers.add(getInitialGameRoundController(currentRoundID + 1));
        initDisplay();
    }

    /**
     * 
     * @return
     */
    public Object[] getValidVacanciesForThisRound(){
        ArrayList<IMP03_Vacancy> tmpVacanciesForThisRound = new ArrayList<IMP03_Vacancy>();

        for(IMP03_Vacancy v : IMP03_Vacancy.getAllAvailableVacanciesFromDB(true)){
            boolean vacancyAlreadyPlayed = false;
            
            for(GameRoundController grc : gameRoundControllers){
                if(grc.getSelectedVacancy() != null && v.getVacancyID() == grc.getSelectedVacancy().getVacancyID()){
                    vacancyAlreadyPlayed = true;
                    break;
                }
            }
            
            if(!vacancyAlreadyPlayed){
                tmpVacanciesForThisRound.add(v);
            }
        }

        return tmpVacanciesForThisRound.toArray();
    }
    
    private GameRoundController getInitialGameRoundController(int roundID){
        return new GameRoundController(true, false, false, false, true, 1, null, null, null, roundID, 0);
    }
    
    /**
     * 
     */
    public void initDisplay(){
        mainPanel.removeAll();
        
        BorderLayout layout = new BorderLayout();
        mainPanel.setLayout(layout);
        GameScreen gameScreen = new GameScreen(this);
        gameScreen.setPlanspielView(planspielView);
        gameScreen.paintScreen();
        mainPanel.add(gameScreen, BorderLayout.CENTER);
        mainPanel.validate();
    }

    /**
     * 
     * @return
     */
    public GameRoundController getCurrentGameRoundController() {
        return gameRoundControllers.get(gameRoundControllers.size() - 1);
    }

	/**
	 * @return the gameRound
	 */
	public GameRound getGameRound() {
		return gameRound;
	}
}
